<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="学号" prop="num">
        <el-input v-model="dataForm.num" placeholder="学号"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="dataForm.realName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="启用状态" prop="status">
        <!-- <el-input v-model="dataForm.status" placeholder="状态  0：禁用   1：正常"></el-input> -->
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择性别">
          <el-option
            v-for="item in options01"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-select v-model="dataForm.grade" placeholder="请选择年级">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍id" prop="dormId">
        <!-- <el-input v-model="dataForm.dormId" placeholder="宿舍id"></el-input> -->
        <el-cascader
          :props="defaultProps"
          v-model="dormValue"
          :options="menus"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      flag: 0,
      defaultProps: {
        children: "children",
        label: "name",
        value: "id",
      },
      dormValue: "",
      menus: [],
      options01: [
        {
          value: 0,
          label: "男",
        },
        {
          value: 1,
          label: "女",
        },
      ],
      options: [
        {
          value: 0,
          label: "大一",
        },
        {
          value: 1,
          label: "大二",
        },
        {
          value: 2,
          label: "大三",
        },
        {
          value: 3,
          label: "大四",
        },
      ],
      value: true,
      visible: false,
      dataForm: {
        userId: 0,
        username: "",
        password: "",
        salt: "",
        email: "",
        mobile: "",
        status: "",
        createUserId: "",
        createTime: "",
        sex: "",
        grade: "",
        dormId: "",
        num: "",
        realName: "",
      },
      dataRule: {
        // username: [
        //   { required: true, message: "用户名不能为空", trigger: "blur" },
        // ],
        // password: [
        //   { required: true, message: "密码不能为空", trigger: "blur" },
        // ],
        // salt: [{ required: true, message: "盐不能为空", trigger: "blur" }],
        // email: [{ required: true, message: "邮箱不能为空", trigger: "blur" }],
        // mobile: [
        //   { required: true, message: "手机号不能为空", trigger: "blur" },
        // ],
        // status: [
        //   {
        //     required: true,
        //     message: "状态  0：禁用   1：正常不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // createUserId: [
        //   { required: true, message: "创建者ID不能为空", trigger: "blur" },
        // ],
        // createTime: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" },
        // ],
        // sex: [
        //   {
        //     required: true,
        //     message: "性别(0:男 1：女)不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // grade: [
        //   {
        //     required: true,
        //     message: "年级（0：大一 1：大二 2：大三 3：大四）不能为空",
        //     trigger: "blur",
        //   },
        // ],
        // dormId: [
        //   { required: true, message: "宿舍id不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getMenus();
  },
  methods: {
    handleChange() {},
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/sys/dormarea/getCategory"),
        method: "get",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据...", data.data);
        this.menus = data.data;
      });
    },

    init(id) {
      // this.flag=id||0;
      this.dataForm.userId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.userId) {
          this.$http({
            url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.userId}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              console.info(data.user.dormId);
              this.dataForm.username = data.user.username;
              this.dataForm.password = data.user.password;
              this.dataForm.salt = data.user.salt;
              this.dataForm.email = data.user.email;
              this.dataForm.mobile = data.user.mobile;
              this.dataForm.status = data.user.status;
              this.dataForm.createUserId = data.user.createUserId;
              this.dataForm.createTime = data.user.createTime;
              this.dataForm.sex = data.user.sex;
              this.dataForm.grade = data.user.grade;
              this.dataForm.dormId = data.user.dormId;
              this.value = this.dataForm.status == 0 ? false : true;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/user/${!this.dataForm.userId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              userId: this.dataForm.userId || undefined,
              // username: this.dataForm.username,
              // password: this.dataForm.password,
              // salt: this.dataForm.salt,
              // email: this.dataForm.email,
              // mobile: this.dataForm.mobile,
              status: this.value == true ? 1 : 0,
              // createUserId: this.dataForm.createUserId,
              // createTime: this.dataForm.createTime,
              sex: this.dataForm.sex,
              grade: this.dataForm.grade,
              dormId: this.dormValue[2],
              mobile: this.dataForm.mobile,
              num: this.dataForm.num,
              realName: this.dataForm.realName,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
